using System;
using System.Data;
using System.Data.SqlClient;
using SystemFrameworks;
using Microsoft.ApplicationBlocks.Data;
using Common; using MinCultura.Sipa.DataAccess.EntidadesArtes;
using SystemFrameworks;

namespace MinCultura.Sipa.EntidadesArtes
{
	/// <summary>
	/// Clase de acceso a datos para la entidad ART_INFRAESTRUCTURA_BOLETERIA
	/// </summary>
	public class InfraestructuraBoleteria 
	{
		#region Private Data Access Atributes

		private const String TVB_ID_PARAM = "@TVB_ID";
		private const String INF_ID_PARAM = "@INF_ID";

		#endregion Private Data Access Atributes

		/// <summary>
		/// Constructor de la clase ARTINFRAESTRUCTURABOLETERIA
		/// </summary>
        public InfraestructuraBoleteria(string strCadenaConecxion)
        {
            ConnectionString = strCadenaConecxion;
        }

        private string _cadenaConexion;

        public string ConnectionString
        {
            get { return _cadenaConexion; }
            set { _cadenaConexion = value; }
        }

		public InfraestructuraBoleteriaData BuscarInfraestructuraBoleteria(decimal parINF_ID)
		{
			InfraestructuraBoleteriaData response = new InfraestructuraBoleteriaData();
			SqlParameter param1 = new SqlParameter(INF_ID_PARAM, parINF_ID);
			SqlParameter[] parameters = {param1};
			SqlHelper.ExecuteDataTable(this.ConnectionString, CommandType.StoredProcedure, "PAS_BUSCAR_ART_INFRAESTRUCTURA_BOLETERIA", response, parameters);
			if (response.Rows.Count == 0)
				return null;
			return response;
		}

		public InfraestructuraBoleteriaData ConsultarInfraestructuraBoleteria(decimal parTVB_ID, decimal parINF_ID)
		{
			InfraestructuraBoleteriaData response = new InfraestructuraBoleteriaData();
			SqlParameter param1 = new SqlParameter(TVB_ID_PARAM, parTVB_ID);
			SqlParameter param2 = new SqlParameter(INF_ID_PARAM, parINF_ID);
			SqlParameter[] parameters = {param1, param2};
			SqlHelper.ExecuteDataTable(this.ConnectionString, CommandType.StoredProcedure, "PAS_CONSULTAR_ART_INFRAESTRUCTURA_BOLETERIA", response, parameters);
			if (response.Rows.Count == 0)
				return null;
			return response;
		}

		public void AdicionarInfraestructuraBoleteria(InfraestructuraBoleteriaData datos, decimal parINF_ID, params SqlTransaction[] transaccion)
		{
			try
			{
				foreach (DataRow row in datos.Rows)
				{
					SqlParameter param1 = new SqlParameter(TVB_ID_PARAM, row[InfraestructuraBoleteriaData.TVB_ID_FIELD]);
					SqlParameter param2 = new SqlParameter(INF_ID_PARAM, parINF_ID);
					SqlParameter[] parameters = {param1, param2};
					if (transaccion.Length > 0)
					{
						SqlHelper.ExecuteNonQuery(transaccion[0], CommandType.StoredProcedure, "PAS_ADICIONAR_ART_INFRAESTRUCTURA_BOLETERIA", parameters);
					}
					else
					{
						SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ADICIONAR_ART_INFRAESTRUCTURA_BOLETERIA", parameters);
					}
				}
			}
			catch (SqlException sqlEx)
			{
				throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx, "PAS_ADICIONAR_ART_INFRAESTRUCTURA_BOLETERIA"), sqlEx);
			}
			catch (Exception ex)
			{
				throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorInsertar", "PAS_ADICIONAR_ART_INFRAESTRUCTURA_BOLETERIA"), ex);
			}
		}

		public void ActualizarInfraestructuraBoleteria(InfraestructuraBoleteriaData datos, decimal parINF_ID, params SqlTransaction[] transaccion)
		{
			try
			{
				if (transaccion.Length > 0)
				{
					EliminarInfraestructuraBoleteria(parINF_ID,transaccion);
					AdicionarInfraestructuraBoleteria(datos, parINF_ID, transaccion);
				}
				else
				{
					EliminarInfraestructuraBoleteria(parINF_ID,transaccion);
					AdicionarInfraestructuraBoleteria(datos, parINF_ID, transaccion);					
				}
			}
			catch (SqlException sqlEx)
			{
				throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx, "ART_INFRAESTRUCTURA_BOLETERIA"), sqlEx);
			}
			catch (Exception ex)
			{
				throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorActualizar", "ART_INFRAESTRUCTURA_BOLETERIA"), ex);
			}
		}

		/// <summary>
		/// Elimina un registro de la entidad Artinfraestructuraboleteria
		/// </summary>
		/// <param name="Id">Identificador del registro
		public void EliminarInfraestructuraBoleteria(decimal parINF_ID,params SqlTransaction[] transaccion)
		{
			try
			{				
				SqlParameter param1 = new SqlParameter(INF_ID_PARAM, parINF_ID);
				SqlParameter[] parameters = {param1};
				if (transaccion.Length > 0)
				{
					SqlHelper.ExecuteNonQuery(transaccion[0], CommandType.StoredProcedure, "PAS_ELIMINAR_ART_INFRAESTRUCTURA_BOLETERIA", parameters);
				}
				else
				{
					SqlHelper.ExecuteNonQuery(this.ConnectionString, CommandType.StoredProcedure, "PAS_ELIMINAR_ART_INFRAESTRUCTURA_BOLETERIA", parameters);
				}
			}
			catch (SqlException sqlEx)
			{
				throw new ApplicationExceptionManager(Utilidad.DisplaySqlErrors(sqlEx, "ART_INFRAESTRUCTURA_BOLETERIA"), sqlEx);
			}
			catch (Exception ex)
			{
				throw new ApplicationExceptionManager(Utilidad.TraerMensaje("gmsgErrorEliminar", "ART_INFRAESTRUCTURA_BOLETERIA"), ex);
			}
		}
	}
}